package org.mcpro.core.filter;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.mcpro.core.bean.ControlObject;
import org.mcpro.core.config.ActionConfig;

/**
 * Servlet Filter implementation class CentreFilter
 */
public class CentreFilter implements Filter {
	// private Map<String, ControlObject> controlConfig = new HashMap<String,
	// ControlObject>();

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {

		chain.doFilter(request, response);
	}

	public void init(FilterConfig config) throws ServletException {
		// 解析配置文件，将配置文件加载到缓存中方便业务处理
		if (config != null) {
			String action = config.getInitParameter("action");
			ServletContext sc = config.getServletContext();
			String path = sc.getRealPath("/");
			action = path + action;
			if (action.endsWith("**.xml")) {
				String filename = action.substring(0, action.length() - 7);
				initConfig(filename, ".xml");
			}
		}
	}


	/**
	 * 初始化控制器配置
	 * 
	 * @param filename
	 *            读取的文件路径
	 * @param postfix
	 *            读取文件的后缀
	 * @author 乔继承
	 * @date 2013年11月5日 上午12:13:13
	 */
	public void initConfig(String filename, String postfix) {

		if (filename == null || "".equals(filename.trim()) || postfix == null
				|| "".equals(postfix.trim())) {
			return;
		}
		File file = new File(filename);
		initConfig(file, postfix);

	}

	private void initConfig(File file, String postfix) {
		if (file.isDirectory()) {
			File[] files = file.listFiles();
			for (File f : files) {
				initConfig(f, postfix);
			}
		} else {
			Document document = null;
			if (file.getName().endsWith(postfix)) {
				SAXReader reader = new SAXReader();
				try {
					document = reader.read(file);
					List<Element> list = document.selectNodes("actions/action");
					if (list != null && list.size() > 0) {
						Map<String, ControlObject> map = ActionConfig
								.getInstance().getControlConfig();
						for (Element element : list) {
							String action = element.valueOf("@name");
							ControlObject controlObject = null;
							List<Element> elements = element.elements();
							if (elements != null && elements.size() > 0) {
								controlObject = new ControlObject();
								for (Element e : elements) {
									if ("method".equals(e.getName())) {
										controlObject.setMethod(e.getText());
									}
									if ("classname".equals(e.getName())) {
										controlObject.setClassname(e.getText());
									}
									if ("viewname".equals(e.getName())) {
										controlObject.setView(e.getText());
									}
								}

							}
							map.put(action, controlObject);

						}
					}
				} catch (DocumentException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public void destroy() {

	}

}
